import java.io.*;
import java.util.Scanner;
public class text1 {
    public static void copyFile(String sourceFileName, String targetFileName)
            throws IOException {
        File sourceFile = new File(sourceFileName);
        File targetFile = new File(targetFileName);
        if (!sourceFile.exists()) {
            System.out.println("文件复制失败！源文件" +
                    sourceFile.getName() + "不存在");
            return;
        }
        if (targetFile.isDirectory()) {
            targetFile = new File(targetFile, sourceFile.getName());
        }
        if (targetFile.exists()) {
            System.out.println("文件进行复制，是否覆盖现有文件" +
                    targetFile.getName()+ "?(Y/N):");
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    System.in));
            String override = in.readLine();
            if (!override.equalsIgnoreCase("Y")) {
                System.out.println("操作已取消!");
                return;
            }
        }
        FileInputStream in = null;
        FileOutputStream out = null;
        in = new FileInputStream(sourceFile);
        out = new FileOutputStream(targetFile);
        byte[] buffer = new byte[8];
        int num;
        while ((num = in.read(buffer)) != -1)
            out.write(buffer, 0, num);
        System.out.println("文件复制成功！");
        System.out.println("源文件：" + sourceFile.getPath());
        System.out.println("目标文件：" + targetFile.getPath());
        in.close();
        out.close();
    }
    public static void main(String[] args) throws IOException {
        String SrcFileName, DesFileName;
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入源文件名： ");
        SrcFileName = scan.nextLine();
        System.out.println("请输入目标文件名： ");
        DesFileName = scan.nextLine();
        text1.copyFile(SrcFileName, DesFileName);
        scan.close();
    }
}
